package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.uitl.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository travelCommentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> lists() {
        return travelCommentRepository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        //创建查询对象
        Query query=new Query();
        //添加条件所有字符串都要判空的
        if (StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        //分页查询
        Pageable pageRequest=PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.DESC,"createTime");
        return DBHelper.query(mongoTemplate,TravelComment.class,query,pageRequest);
    }
    //添加一个游记回复
    @Override
    public void commentAdd(TravelComment travelComment, UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelComment.setCreateTime(new Date());
        //判断,维护状态
        String refid = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(refid)){
            travelComment.setRefComment(this.get(refid));
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }else {
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        this.save(travelComment);
    }

    @Override
    public List<TravelComment>  queryByTravelId(String travelId) {
        return travelCommentRepository.findByTravelId(travelId);
    }


}
